#define _CRT_SECURE_NO_WARNINGS 1

int TreeSize(struct TreeNode* root)
{
    // return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    if (root == NULL)
        return 0;
    return TreeSize(root->left) + TreeSize(root->right) + 1;
}


void _preorderTraversal(struct TreeNode* root, int* arr, int* pi)
{
    if (root == NULL)
        return;

    arr[(*pi)++] = root->val;

    _preorderTraversal(root->left, arr, pi);
    _preorderTraversal(root->right, arr, pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = TreeSize(root);
    int* arr = (int*)malloc(*returnSize * sizeof(int));

    int i = 0;
    _preorderTraversal(root, arr, &i);
    return arr;

}